﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QLK.Database.DALs
{
    public class PriceDAL
    {
        private ManageStoreDataContext db;

        public PriceDAL()
        {
            db = new ManageStoreDataContext();
        }

        public List<Price> PriceGetAll()
        {
            var price = from p in db.Prices
                        orderby p.PriceID
                        select p;

            return price.ToList<Price>();
        }

        public Price PriceGetByID(int priceID)
        {
            Price price = db.Prices.Single<Price>(p => p.PriceID == priceID);

            return price;
        }

        public bool PriceInsert(Price price)
        {
            try
            {
                db.Prices.InsertOnSubmit(price);
                db.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return false;
            }
        }

        public bool PriceUpdate(Price price)
        {
            Price prices = db.Prices.Single<Price>(p => p.PriceID == price.PriceID);

            if (prices == null)
                return false;

            prices.ProductID = price.ProductID;
            prices.Price1 = price.Price1;
            prices.Active = price.Active;

            try
            {
                db.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return false;
            }
        }

        public bool PriceDeleteByID(int priceID)
        {
            Price price = db.Prices.Single<Price>(p => p.PriceID == priceID);

            if (price == null)
                return false;

            db.Prices.DeleteOnSubmit(price);

            try
            {
                db.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return false;
            }
        }

        public bool PriceCheckPriceIsActive(int ProductID)
        {
            Price price = (from p in db.Prices
                           where p.ProductID == ProductID && p.Active == true
                           select p).Single<Price>();

            if (price == null)
                return true;
            return false;
        }
    }
}
